import {
	apiConfig,
	apiLang,
	apiComplexSimpleChinese
} from '@/api/app'
import {
	apiUserCentre,
	apiDistributionCode
} from '@/api/user'
import {
	apiCloudShopBindCode,
} from "@/api/cloudshop"
import {
	CONFIG,
	USER_INFO,
	TOKEN,
	INVITE_CODE,
	LANG_TYPE,
} from '@/config/cachekey';
import wechath5 from '@/utils/wechath5'
import Cache from '@/utils/cache'
import {
	zhTran
} from '@/utils/langs'
import {
	router
} from '@/router'
import {
	getClient
} from '@/utils/tools'

const def_Psi_Screen = {
	main_bg_color: '#4073FA',
	assist_bg_color: "#ECF1FF",
	main_font_color: '#fff',
	assist_font_color: '#4073FA',
	banner_images: [],
	menu_list: {
		'purchase': {
			id: 'purchase',
			icon: 'resource/image/shopapi/psi/purchase.png',
			title: '采购订单',
			remark: '',
		},
		'wholesale': {
			id: 'wholesale',
			icon: 'resource/image/shopapi/psi/wholesale.png',
			title: '批发订单',
			remark: '',
		},
		'retail': {
			id: 'retail',
			icon: 'resource/image/shopapi/psi/retail.png',
			title: '零售订单',
			remark: '',
		},
		'warehouse_stock': {
			id: 'warehouse_stock',
			icon: 'resource/image/shopapi/psi/warehouse-stock.png',
			title: '仓库库存',
			remark: '销售报表查看',
		},
		'stock_code': {
			id: 'stock_code',
			icon: 'resource/image/shopapi/psi/stock_code.png',
			title: '编码列表',
			remark: '产品编码查看',
		},
		'instock': {
			id: 'instock',
			icon: 'resource/image/shopapi/psi/instock.png',
			title: '入库单据',
			remark: '入库单据查看',
		},
		'outstock': {
			id: 'outstock',
			icon: 'resource/image/shopapi/psi/outstock.png',
			title: '出库单据',
			remark: '出库单据查看',
		},
		'client': {
			id: 'client',
			icon: 'resource/image/shopapi/psi/client.png',
			title: '客户列表',
			remark: '客户列表查看',
		},
		'clerk': {
			id: 'clerk',
			icon: 'resource/image/shopapi/psi/clerk.png',
			title: '职员列表',
			remark: '职员列表信息',
		},
		'next_distributor': {
			id: 'distributor',
			icon: 'resource/image/shopapi/psi/next_distributor.png',
			title: '下级分销商',
			remark: '下级分销商信息',
		},
		'setting': {
			id: 'setting',
			icon: 'resource/image/shopapi/psi/setting.png',
			title: '资料设置',
			remark: '查看/编辑资料信息',
		}
	}
}

const state = {
	config: Cache.get(CONFIG) || {},
	userInfo: {},
	token: Cache.get(TOKEN) || null,
	client: getClient() || null,
	wechat_config_off: 0,
	psiScreenData: zhTran(def_Psi_Screen),
	imghead: 'https://xtc.kshop.cc/resource/image/shopapi/mobile_img/',
	// login_fend_off: 2
};

const mutations = {
	setConfig(state, data) {
		state.config = data
		Cache.set(CONFIG, data);
	},
	login(state, data) {
		state.token = data.token;
		Cache.set(TOKEN, data.token);
		// 登录完成绑定邀请码
		const code = Cache.get(INVITE_CODE)
		if (code) {
			apiDistributionCode({
				code,
				hide: 1
			}).finally(() => {
				Cache.remove(INVITE_CODE)
			})
		}
		const clsp_id = Cache.get('clsp_id')
		if (clsp_id) {
			apiCloudShopBindCode({
				clsp_id: clsp_id,
			}).then(() => {
				this.$Router.replaceAll({
					path: '/bundle2/pages/cloud_shop/diyshop/index',
					query: {
						clspId: clsp_id
					}
				})
			})
		}
	},
	logout(state) {
		state.token = '';
		state.userInfo = {}
		Cache.remove(TOKEN);
	},
	wechatConfigOff(state, data) {
		state.wechat_config_off = data
	},
	// loginFendOff(state, data) {
	// 	state.login_fend_off = data
	// },
	setUserInfo(state, data) {
		state.userInfo = data
	},
	setPsiScreenConfig(state, data) {
		state.psiScreenData = data
	},
};

const actions = {
	getConfig({
		state,
		commit
	}) {
		return apiConfig().then(res => {
			commit('setConfig', res)
			return Promise.resolve(res)
		}).catch(() => {
			return Promise.reject()
		})
	},
	getLang({
		state,
		commit,
		getters
	}) {
		apiLang().then(res => {
			Cache.set(LANG_TYPE, res.user_lang ? 't' : 's')
		})
	},
	getUser({
		state,
		commit
	}) {
		return new Promise((resolve, reject) => {
			apiUserCentre({
				clsp_id: Cache.get('clsp_id')
			}).then(res => {
				commit('setUserInfo', res)
				resolve(res)
			}).catch(() => {
				reject()
			})
		})
	},
	setWxShare({
		state
	}, opt) {
		// #ifdef H5
		const {
			share_image,
			share_intro,
			share_title
		} = state.config
		const inviteCode = state.userInfo.code
		const href = window.location.href
		const sym = href.includes('?') ? '&' : '?'
		const option = {
			shareTitle: share_title,
			shareLink: inviteCode ? `${href}${sym}invite_code=${inviteCode}` : href,
			shareImage: share_image,
			shareDesc: share_intro
		}
		wechath5.share(Object.assign(option, opt))
		// #endif
	},
};

export default {
	state,
	mutations,
	actions
};